package com.icexxx.litchi;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LitchiShutdown {

    public static String machining(Integer port) {
        String wholeString = wholeString();
        String hex = hex(wholeString);
        String urlBase = "http://localhost";
        String url = urlBase + ":" + port + "/" + hex;
        return url;
    }

    private static String wholeString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        String salt = "<<litchi>>";
        String cmd = "shutdown";
        StringBuilder sb = new StringBuilder();
        sb.append(salt);
        sb.append(date);
        sb.append(cmd);
        String wholeString = sb.toString();
        return wholeString;
    }

    private static String hex(String str) {
        if (str == null || "".equals(str)) {
            return "";
        }
        byte[] bys = null;
        try {
            bys = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return hex(bys);
    }

    private static String hex(byte[] bys) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bys.length; i++) {
            byte byteCur = bys[i];
            String hexString = Integer.toHexString(byteCur);
            sb.append(hexString);
        }
        return sb.toString();
    }
}
